#include <rtthread.h>
#include <string.h>
#include <finsh.h>
#include <../utils/cJSON.h>

#define CONFIG_FILE ("/config.json")


static void resetConfig(int argc, char **argv)
{
    int fdConfig = -1;
    fdConfig = open(CONFIG_FILE, O_RDWR | O_TRUNC);

    if (fdConfig >= 0)
    {
        rt_kprintf("\nreset config file\n");

        cJSON *root = cJSON_CreateArray();
        cJSON *com1, *com2, *com3, *com4, *com5;
        com1 = cJSON_CreateObject();
        com2 = cJSON_CreateObject();
        com3 = cJSON_CreateObject();
        com4 = cJSON_CreateObject();
        com5 = cJSON_CreateObject();

        // com1
        cJSON_AddNumberToObject(com1, "baudrate", 614400);  // 614400, 115200
//        cJSON_AddNumberToObject(com1, "stopbit", 1);
//        cJSON_AddNumberToObject(com1, "databit", 8);
        cJSON_AddNumberToObject(com1, "parity", 0);
        cJSON_AddNumberToObject(com1, "switchx", 1);
        cJSON_AddNumberToObject(com1, "period", 100); // 10ms 20ms 50ms 100ms
        cJSON_AddNumberToObject(com1, "datalen", 10); // 10 to 800 by 1, for com1 recv only
        cJSON_AddNumberToObject(com1, "dl_send", -1); // data len for send, no use here
        cJSON_AddNumberToObject(com1, "dl_recv", -1); // data len for recv, no use here
        cJSON_AddNumberToObject(com1, "head_1", -1);  // no use here
        cJSON_AddNumberToObject(com1, "head_2", -1);  // no use here
        cJSON_AddNumberToObject(com1, "head_3", -1);  // no use here
        cJSON_AddNumberToObject(com1, "ind", 0);


        // com2
        cJSON_AddNumberToObject(com2, "baudrate", 9600); // 9600 ~ 921600
//        cJSON_AddNumberToObject(com2, "stopbit", 1);
//        cJSON_AddNumberToObject(com2, "databit", 8);
        cJSON_AddNumberToObject(com2, "parity", 0);
        cJSON_AddNumberToObject(com2, "switchx", 1);
        cJSON_AddNumberToObject(com2, "period", 20); // 10ms 20ms
        cJSON_AddNumberToObject(com2, "datalen", -1); // no use here
        cJSON_AddNumberToObject(com2, "dl_send", 10); // data len for send, 10~120
        cJSON_AddNumberToObject(com2, "dl_recv", 10); // data len for recv, 10~120
        cJSON_AddNumberToObject(com2, "head_1", 126);
        cJSON_AddNumberToObject(com2, "head_2", 126);
        cJSON_AddNumberToObject(com2, "head_3", 126);
        cJSON_AddNumberToObject(com2, "ind", 1);

        // com3
        cJSON_AddNumberToObject(com3, "baudrate", 19200); // 9600 ~ 921600
//        cJSON_AddNumberToObject(com3, "stopbit", 1);
//        cJSON_AddNumberToObject(com3, "databit", 8);
        cJSON_AddNumberToObject(com3, "parity", 0);
        cJSON_AddNumberToObject(com3, "switchx", 1);
        cJSON_AddNumberToObject(com3, "period", 20); // 10ms 20ms
        cJSON_AddNumberToObject(com3, "datalen", -1); // no use here
        cJSON_AddNumberToObject(com3, "dl_send", 10); // data len for send, 10~120
        cJSON_AddNumberToObject(com3, "dl_recv", 10); // data len for recv, 10~120
        cJSON_AddNumberToObject(com3, "head_1", 126);
        cJSON_AddNumberToObject(com3, "head_2", 126);
        cJSON_AddNumberToObject(com3, "head_3", 126);
        cJSON_AddNumberToObject(com3, "ind", 2);

        // com4
        cJSON_AddNumberToObject(com4, "baudrate", 9600); // 9600 ~ 921600
//        cJSON_AddNumberToObject(com4, "stopbit", 1);
//        cJSON_AddNumberToObject(com4, "databit", 8);
        cJSON_AddNumberToObject(com4, "parity", 0);
        cJSON_AddNumberToObject(com4, "switchx", 1);
        cJSON_AddNumberToObject(com4, "period", 20); // 10ms 20ms
        cJSON_AddNumberToObject(com4, "datalen", -1); // no use here
        cJSON_AddNumberToObject(com4, "dl_send", 10); // data len for send, 10 ~ 120
        cJSON_AddNumberToObject(com4, "dl_recv", 10); // data len for recv, 10 ~ 350
        cJSON_AddNumberToObject(com4, "head_1", 126);
        cJSON_AddNumberToObject(com4, "head_2", 126);
        cJSON_AddNumberToObject(com4, "head_3", 126);
        cJSON_AddNumberToObject(com4, "ind", 3);

        // com5
        cJSON_AddNumberToObject(com5, "baudrate", 9600); // 9600 ~ 921600
//        cJSON_AddNumberToObject(com5, "stopbit", 1);
//        cJSON_AddNumberToObject(com5, "databit", 8);
        cJSON_AddNumberToObject(com5, "parity", 0);
        cJSON_AddNumberToObject(com5, "switchx", 1);
        cJSON_AddNumberToObject(com5, "period", 20); // 10ms 20ms
        cJSON_AddNumberToObject(com5, "datalen", -1); // no use here
        cJSON_AddNumberToObject(com5, "dl_send", 10); // data len for send, 10 ~ 120
        cJSON_AddNumberToObject(com5, "dl_recv", 10); // data len for recv, 10 ~ 350
        cJSON_AddNumberToObject(com5, "head_1", 126);
        cJSON_AddNumberToObject(com5, "head_2", 126);
        cJSON_AddNumberToObject(com5, "head_3", 126);
        cJSON_AddNumberToObject(com5, "ind", 4);

        cJSON_AddItemToArray(root, com1);
        cJSON_AddItemToArray(root, com2);
        cJSON_AddItemToArray(root, com3);
        cJSON_AddItemToArray(root, com4);
        cJSON_AddItemToArray(root, com5);

        cJSON *udp = cJSON_CreateObject();
        cJSON_AddNumberToObject(udp, "ind", 5);
        cJSON_AddNumberToObject(udp, "len", 50);  /// unit is Mbit
        cJSON_AddItemToArray(root, udp);

        char* initFileContent = cJSON_Print(root);
        write(fdConfig, initFileContent, strlen(initFileContent));
        fsync(fdConfig);
        free(initFileContent);

//        cJSON_Delete(root);
//        cJSON_Delete(com1);
//        cJSON_Delete(com2);
//        cJSON_Delete(com3);
//        cJSON_Delete(com4);
//        cJSON_Delete(com5);

        close(fdConfig);
        fdConfig = -1;

        updateRoot();
    }
    else
    {
        rt_kprintf("\nreset config file failed.\n");
    }

    return;
}
MSH_CMD_EXPORT(resetConfig, reset config file);


